<template>
	<view>
		<myheader :leftType="3" leftUrl="/pages/find/index" :noLeft="false" showTitle="加入群组" showBackground="#EAECEF">
	</myheader>
	<view @tap="join" class="join">
		加入群组邀请
	</view>
	</view>
</template>


<script>
import {
	mapGetters
} from "vuex";
	import myheader from '../../components/header/index.vue';
	export default {
		components: {
			myheader
		},
		data() {
			return {
				groupToken: ""
			}
		},
		onShow() {},
		computed: {
			...mapGetters(["nickname"])
		},
		onLoad(e) {
			if (e.groupToken != null && e.groupToken != "") {
				this.groupToken = e.groupToken;
			} else {
				uni.switchTab({
					url: "/pages/find/index",
				});
			}
		},
		methods: {
			join() {
				if (!this.nickname) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else {
					this.addGroup()
				}
			},
			addGroup() {
				if (this.groupToken != "") {
					this.$store.commit('setGroupToken',this.groupToken);
					uni.showLoading({
						title: '正在加入群组中...',
						mask: true
					});
					this.$H.get("portal/group/joinGroup?groupToken=" + this.groupToken).then(res => {
						this.$store.commit('clearGroupToken');
						if (res.data.code == 0) {
							uni.showToast({
								title: "加入群组成功",
								icon: "none"
							})
							setTimeout(function(){
								uni.redirectTo({
									url: "/pages/my/groupDetail?groupId="+res.data.groupId
								});
							},1000)
						}
						if (res.data.code == -1) {
							uni.showToast({
								title: "您已加入该群组",
								icon: "none"
							})
							setTimeout(function(){
								uni.redirectTo({
									url: "/pages/my/groupDetail?groupId="+res.data.groupId
								});
							},1000)

						}
						if (res.data.code == -2) {
							uni.showModal({
								content: '邀请已过期！请联系群主重发邀请',
								confirmColor: "#E02828",
								showCancel: false,
								confirmText: '回首页',
								success: function (res) {
									if (res.confirm) {
										uni.reLaunch({
					url: "/pages/index/index"
				})
									} else if (res.cancel) {
										return false;
									}
								},
							});
						}
					})
				}
			}
		}
	}
</script>
<style>
.join {
	border-radius: 20rpx;
	margin: 300px auto 0;
	display:flex;justify-content: center;align-items: center; width:670rpx;height:120rpx;background-color: #1abc9c;color:#fff;font-size: 20px;
}
</style>